package com.feng.gulimall.product.service.impl;

import com.feng.common.utils.R;
import com.feng.gulimall.product.entity.AttrEntity;
import com.feng.gulimall.product.entity.CategoryEntity;
import com.feng.gulimall.product.service.AttrService;
import com.feng.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.common.utils.PageUtils;
import com.feng.common.utils.Query;

import com.feng.gulimall.product.dao.AttrGroupDao;
import com.feng.gulimall.product.entity.AttrGroupEntity;
import com.feng.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /*
    * 根据params 和 catlogId(三级分类的id)进行分页查询
    *  查询全部也带有模糊查询功能
    * */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //如果含有三级分类的id信息  其中前端查询包含一个key检索条件信息
        String key = (String) params.get("key");
        //select * from attr_group where catlogId = ? and (attr_group_id = key or attr_group_name like %key%)
        //创建一个QueryWrapper对象,因为此时catlogId肯定不为空,我们直接带上
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        //判断是否包含catlogId的信息,如果没有则默认参数为0
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }else {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper.eq("catelog_id",catelogId));
            return new PageUtils(page);
        }
    }

    /*
    * 查询指定分类下的所有属性分组信息以及属性分组对应的属性信息
    * */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1.查询分组信息
        List<AttrGroupEntity> entities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //2.查询属性分组对应的属性信息
        List<AttrGroupWithAttrsVo> collect = entities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrsVo);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());
        return collect;
    }


}